{% from 'admin/common/_macro.html' import  terms_render_page_data%}
{%  extends "admin/common/admin_base.html" %}
{% block title -%}
   {{ page_title }} 管理
{% endblock %}
{% block page_title -%}{{ page_title }} 管理{% endblock %}
{% block page_btn_name -%}{{ page_title }} 列表{% endblock %}
{% block content -%}
<section class="content">
<div class="row">
        <div class="col-md-12">
                <form class="form-horizontal" method="POST" action="{{ url_for('adminterms.edit',taxonomy=taxonomy) }}" onsubmit="return false" >
                    {% if info  -%}
                        <input type="hidden" name="id" value="{{ info.id }}">
                    {%- endif %}
                    <div class="nav-tabs-custom">
                        <ul class="nav nav-tabs">
                            <li class="active"><a href="#tab1" data-toggle="tab">基本参数</a></li>
                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane active" id="tab1">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">名称</label>
                                    <div class="col-sm-7"><input class="form-control" name="name" value="{{ info.name|default("") }}" placeholder="这将是它在站点上显示的名字。"></div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">别名</label>
                                    <div class="col-sm-7"><input class="form-control" name="slug" value="{{ info.slug|default("") }}" placeholder="别名”是在URL中使用的别称，它可以令URL更美观。通常使用小写，只能包含字母，数字和连字符（-）"></div>
                                </div>
                            </div>
                            {% if taxonomy != "post_tag" %}
                            <div class="form-group">
                                <label class="col-sm-2 control-label">父级分类目录</label>
                                <div class="col-sm-7">
                                    <select class="form-control select2" name="parent" style="width:100%;">
                                        <option value="0" >无</option>
                                        {% for vo in parentData %}
                                            <option value="{{ vo.id }}" level="{{ vo.level }}" {% if vo.level == 2 or (info and vo.id == info.term_taxonomy.id)%}disabled="disabled"{% endif %} {% if info and info.term_taxonomy.parent == vo.id %}selected="selected"{% endif %}>
                                                {%- if vo.level == 2 %}　　│　　├ {{ vo.terms[0].name }}{% endif -%}
                                                {%- if vo.level == 1 %}　　├ {{ vo.terms[0].name }}{% endif -%}
                                                {%- if vo.level == 0 %}{{ vo.terms[0].name }}{% endif -%}
                                            </option>
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>
                            {% endif %}

                            {% if taxonomy != "post_tag" %}
                            <div class="form-group">
                            <label class="col-sm-2 control-label">图片描述</label>
                            <div class="col-sm-7">
                                <div class="input-group">
                                    <input class="form-control" name="img"  {% if info %} value="{{ info.term_taxonomy.img }}" {% endif %}placeholder="描述只会在一部分主题中显示" >
                                    <span class="input-group-btn">
                                        <a href="{% if info %}{{ no_img(info.term_taxonomy.img) }}{% else %}{{ no_img('')}}{% endif %}" target="_blank" >
                                            <img src="{% if info %}{{ no_img(info.term_taxonomy.img) }}{% else %}{{ no_img('')}}{% endif %}" style="height:34px; width:68px;" />
                                        </a>
                                        <button class="btn btn-success btn-flat up_img" type="button"><i class="fa fa-cloud-upload"> 上传</i></button>
                                    </span>
                                </div>
                            </div>
                            </div>
                            {% endif %}
                        </div>
                        <div class="box-footer">
                            <div class="col-sm-2"></div>
                            <div class="col-sm-7">
                                <div class="btn-group pull-right">
                                    <button type="submit" class="btn btn-info pull-right submits" data-loading-text="&lt;i class='fa fa-spinner fa-spin '&gt;&lt;/i&gt;添加新标签">{% if info %}修改{% else %}增加{% endif %}{{ page_title }}</button>
                                </div>
                                <div class="btn-group pull-left">
                                    <button type="reset" class="btn btn-warning">重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
        </div>
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title"></h3>
                    <div class="pull-left">
                        {{authAction(url_for('adminterms.edit',taxonomy=taxonomy), 'create')}}
                        {{authAction(url_for('adminterms.delete',taxonomy=taxonomy),cationType='delete_all')}}
                    </div>
                    {% if taxonomy == "post_tag" %}
                        {% include 'admin/common/search.html' %}
                    {% endif %}
                </div>
                <div class="box-body table-responsive">
                    <table class="table table-bordered table-hover table-sort">
                        <tr>
                            <th width="35"><input type="checkbox" class="minimal checkbox-toggle"></th>
                            <th>名称</th>
                            {% if taxonomy != "post_tag" %}
                            <th>图像描述</th>
                            {% endif %}
                            <th>别名</th>
                            <th>总数</th>
                            <th width="202">操作</th>
                        </tr>
                        {% if taxonomy == "post_tag" %}
                            {% for v in data.items -%}
                                {% if v.term_taxonomy.taxonomy == get_taxonomy_id %}
                                <tr>
                                    <td style="vertical-align:middle"><input type="checkbox" name="id[]" value="{{ v.id }}" class="minimal"></td>
                                    <th>{{ v.name }}</th>
                                    {% if taxonomy != "post_tag" -%}
                                    <td>{{ v.term_taxonomy.img }}</td>
                                    {%- endif %}
                                    <td>{{ v.slug }}</td>
                                    <td>{{ v.term_taxonomy.count }}</td>
                                    <td style="vertical-align:middle">{{authAction(url_for('adminterms.edit',taxonomy=taxonomy,id=v.id), 'edit')}}  {{authAction(url_for('adminterms.delete',taxonomy=taxonomy),cationType='delete',param=v.id)}} </td>
                                </tr>
                                {% endif %}
                            {%- endfor %}
                        {% else %}
                            {% for v2 in parentData -%}
                                <tr>
                                    <td style="vertical-align:middle"><input type="checkbox" name="id[]" value="{{ v2.terms[0].id }}" class="minimal"></td>
                                    <th>
                                    {%- if v2.level == 2 %}　　│　　├ {{ v2.terms[0].name }}{% endif -%}
                                    {%- if v2.level == 1 %}　　├ {{ v2.terms[0].name }}{% endif -%}
                                    {%- if v2.level == 0 %}{{ v2.terms[0].name }}{% endif -%}
                                    </th>
                                    <td style="vertical-align:middle"><img src="{{ no_img(v2.img) }}" style="width:40px;border-radius:40%;" /></td>
                                    <td>{{ v2.terms[0].slug }}</td>
                                    <td>{{ v2.count }}</td>
                                    <td style="vertical-align:middle">{{authAction(url_for('adminterms.edit',taxonomy=taxonomy,id=v2.terms[0].id), 'edit')}}  {{authAction(url_for('adminterms.delete',taxonomy=taxonomy),cationType='delete',param=v2.id)}} </td>
                                </tr>
                            {%- endfor %}
                        {% endif %}
                    </table>
                </div>
                {% if taxonomy == "post_tag" %}
                    {{ terms_render_page_data(data,'adminterms.edit',taxonomy) }}
                {% endif %}
            </div>
        </div>
</div>
</section>
    <script type="text/javascript">
        $(function(){
            /*ajax页面加载icheck，有该控件的页面才需要*/
            $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
                checkboxClass: 'icheckbox_minimal-blue',
                radioClass: 'iradio_minimal-blue'
            });
            $(".select2").select2({language:"zh-CN"});
            /*全选-反选*/
            $('.checkbox-toggle').on('ifChecked', function(event){
                var _this = $(this);
                var _table = _this.closest('.table');
                _table.find("tr td input[type='checkbox']").iCheck("check");
            });
            $('.checkbox-toggle').on('ifUnchecked', function(event){
                var _this = $(this);
                var _table = _this.closest('.table');
                _table.find("tr td input[type='checkbox']").iCheck("uncheck");
            });

            $('#fileupload_').fileupload({
                dataType: 'json',
                done: function (e, data) {
                    if (data.result.code == 200) {
                        var up_url = data.result.data.file_path.trim();
                        obj.parent().prev().val(up_url );
                        if (obj.prev().children('img').length>0) {
                            obj.prev().attr('href',up_url );
                            obj.prev().find('img').attr('src',up_url );
                            $.amaran({'message':data.result.info});
                        }
                    } else {
                        $.amaran({'message':data.result.info});
                    }
                }
            });
            $(".up_img").on('click',function(){
                obj = $(this);
                $('#fileupload_').trigger('click');
            })
        })
    </script>
{% endblock %}